package com.jyb.qiyefuwu.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.ViewModelProvider;

import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;

import me.yokeyword.fragmentation.SupportFragment;

/**
 * @author: 杨畔
 * @date: 2021/2/20 15:23
 * @Description: fragment基类
 */
public abstract class MBaseFragment<VM extends BaseViewModel, VDB extends ViewDataBinding>
        extends SupportFragment implements View.OnClickListener {

    protected VM mViewModel = null;
    protected VDB mBinding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //初始化我们的binging
        mBinding = DataBindingUtil.inflate(inflater, layoutId(), container, false);
        //创建我们的ViewModel
        createViewModel();
        //给binding加上感知生命周期
        mBinding.setLifecycleOwner(this);
        init();
        //绑定EventBus
        if (isUseEventBus() && !EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        return mBinding.getRoot();
    }


    /**
     * 获得viemodelClass
     * @return
     */
    protected abstract Class<VM> createViewModelClass();
    /**
     * 获取BR
     */
    protected abstract int initBR();

    /**
     *布局文件
     */
    protected abstract int layoutId();

    /**
     * 初始化逻辑
     */
    protected abstract void init();
    /**
     * 是否使用eventBus
     */
    protected boolean isUseEventBus() {
        return false;
    }

    @Override
    public void onClick(View view) {

    }

    /**
     * 创建viewmodel
     */
    private  void createViewModel(){
        if(createViewModelClass()!=null){
            mViewModel = new ViewModelProvider(this).get(createViewModelClass());
            if(initBR()>0){
                mBinding.setVariable(initBR(), mViewModel);
            }
            initObserve();
        }
    }

    /**
     * 初始化默认Observe
     */
    private void initObserve(){
        mViewModel.mToast.observe(getViewLifecycleOwner(),message -> MToast.showLongToast(getActivity(),message));
        mViewModel.mLoading.observe(getViewLifecycleOwner(),type->{
            switch (type){
                case BaseViewModel.SHOW_LODING:
                    DialogHelper.showProgressDialog(getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                    break;
                case BaseViewModel.DISMISS_LODING:
                    DialogHelper.dismissProgressDialog();
                    break;
                case BaseViewModel.FINISH_AND_DISMISS_LODING:
                    DialogHelper.dismissProgressDialog();
                    getActivity().finish();
                    break;
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //解绑eventBus
        if (isUseEventBus() && EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
